﻿using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using O.M.Production.Application.IService;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using O.M.Production.Infrastuecure.IRepository;
using O.M.Production.Infrastuecure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace O.M.Production.Application.Service
{
    /// <summary>
    /// 生产计划表服务接口实现类
    /// </summary>
    public class production_plan_Service : BaseServices<production_plan, string>, I_production_plan_Service
    {
        private readonly I_production_plan_Repository _production_plan_Repository;
        public production_plan_Service(I_production_plan_Repository i_Production_Plan_Repository) : base(i_Production_Plan_Repository)
        {
            _production_plan_Repository = i_Production_Plan_Repository;
        }
        /// <summary>
        /// 分页显示-生产计划列表
        /// </summary>
        /// <param name="production_plan_id_Name">生产计划id或名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiResult> Get_production_plan(string production_plan_id_Name, int pageSize, int pageIndex)
        {
            try
            {
                ApiResult apiResult = new ApiResult();
                Expression<Func<production_plan, bool>> where = i => true;
                if (!string.IsNullOrEmpty(production_plan_id_Name))
                {
                    where = i => i.production_plan_id.Contains(production_plan_id_Name) || i.planning_theme.Contains(production_plan_id_Name);
                }
                var list = await _production_plan_Repository.GetShowQueryableAsync(pageSize, pageIndex, where);
                apiResult.totalCount = list.TotalCount;
                apiResult.pageCount=list.PageCount;
                apiResult.Data = list.Data.OrderByDescending(x => x._add_time);
                return apiResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }

   
}
